package com.dfrz.controller;

import com.dfrz.common.ResultEnum;
import com.dfrz.entity.Result;
import com.dfrz.entity.User;
import com.dfrz.service.UserService;
import com.dfrz.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


@RestController
public class UserController{
    @Resource
    UserService userService;
    @GetMapping("/getuser")
    public Result selectUserByid(Integer uid){
        User user=userService.selectUserByid(uid);
        return Result.success(user);
    }
    //账号密码登录
    @GetMapping("/loginbypwd")
    public Result loginbypwd( String account, String pwd){
        User user=userService.loginbypwd(account,pwd);
        if(user == null){
            return Result.error(412,"账号或密码错误");
        }else {
            //获取jwt令牌
            Map<String,Object> map = new HashMap<>();
            map.put("account",user.getAccount());
            map.put("career", user.getCareer());
            String jwt = JwtUtil.generateJwt(map);
            user.setToken(jwt);
            return Result.success(user);
        }
    }

    //获取验证码
    private static String nowtemplateParam;  //获取验证码的手机号
    private static String nowphonenumber;    //该次获取的验证码
    @GetMapping("/sendvalidcode")
    public Result getvalidcode(String phone) throws Exception {
        Map<String, String> map = new HashMap<>();
        Random random = new Random();
        String templateParam = "";
        for (int i = 0; i < 6; i++) {
            templateParam += random.nextInt(9);
        }
        //phoneNumber 手机号      signName 签名     templateCode 模板    templateParam 模板参数
        map.put("phoneNumber",phone);
        map.put("signName","MikuFun网站");
        map.put("templateCode","SMS_474210240");
        map.put("templateParam",templateParam);
        com.dfrz.utils.MessageUtil.sendSms(map);
        nowtemplateParam=templateParam;
        nowphonenumber=phone;
        return Result.success(ResultEnum.SUCCESS);
    }
    //验证码登录/注册
    @GetMapping("/loginbyvalidcode")
    public Result loginbyvalidcode(String phone, String validcode){
        if(phone.equals(nowphonenumber)&&validcode.equals(nowtemplateParam)){
           User user=userService.selectUserbyaccount(phone);
           if(user!=null){
               Map<String,Object> map = new HashMap<>();
               map.put("account",user.getAccount());
               map.put("career", user.getCareer());
               String jwt = JwtUtil.generateJwt(map);
               user.setToken(jwt);
               return Result.success(user);
           }else {
               User user1=new User();
               user1.setAccount(phone);
               user1.setPassword(phone);
               user1.setUsername("用户"+nowtemplateParam);
               user1=userService.signin(user1);
               Map<String,Object> map = new HashMap<>();
               map.put("account",user.getAccount());
               map.put("career", user.getCareer());
               String jwt = JwtUtil.generateJwt(map);
               user1.setToken(jwt);
               return Result.success(user1);
           }
        }
        return Result.error(400,"失败");
    }
    //会员
    public Result vip(Integer uid){
        userService.vip(uid);
        return Result.success(true);
    }

    @GetMapping("getUserInfo/{uid}")
    public Result getUserInfo(@PathVariable Integer uid){
        return Result.success(userService.getUserById(uid));
    }
}
